<template>
	<view>
		<!-- 顶部导航 -->
		<fa-navbar title="寄添加" ref="navbar" ></fa-navbar>
		<view class="content">
			<u-form :model="form" ref="uForm">
				<u-form-item label="选择产品" prop="name" label-width="200">
					<u-input v-model="form.name" placeholder="请选择" @input="handleSearchInput1"/>
					<u-select v-model="show" mode="single-column" :list="list1" @confirm="confirm"></u-select>
				</u-form-item>
				<!-- <u-form-item label="名称" prop="name" label-width="140">
					<u-input v-model="form.name" />
				</u-form-item> -->
				<u-form-item label="数量" prop="num" label-width="140">
					<u-input v-model="form.num" />
				</u-form-item>
				<u-form-item label="上传图片" prop="jzqy" label-width="140">
					<u-upload :action="action" :auto-upload="true" @on-success='handleSuccess' @on-remove="handleRemove"></u-upload>
				</u-form-item>
				<!-- <u-form-item label="选择商品" label-width="140"></u-form-item>
				<u-checkbox-group @change="checkboxGroupChange">
				
					<u-checkbox v-model="item.checked" v-for="(item, index) in list_goods" :key="index"
						:name="item.goods_id" style="margin-right: 10px;">
						<view style="border-bottom: 1px solid #eee;margin-top: 10px;">
							<image :src="item.image" mode="" style="width: 100px;height: 100px;margin-left: 15px;">
							</image>
							<view class=""
								style="text-align: center;width: 130px;height: 32px;line-height: 32px;font-size: 14px;">
								{{item.goods_name}}
							</view>
							<view class=""
								style="text-align: center;width: 130px;height: 36px;line-height: 36px;font-size: 14px;">
								<span style="float: left;margin-right: 10px;">数量:</span><u-input
									v-model="form.goods_num[item.goods_id]"  placeholder="请填写数量" />
							</view>
						</view>
					</u-checkbox>
				
				</u-checkbox-group> -->
			</u-form>
			<u-button @click="submit" :custom-style="customStyle">添加</u-button>
		</view>
		<u-toast ref="uToast" />
		<!-- 底部导航 -->
		<fa-tabbar></fa-tabbar>
	</view>
</template>

<script>
	import { archives } from '@/common/fa.mixin.js';
	export default {
		mixins: [archives],
		data() {
			return {
				action: 'http://leiyu99.com/api/common/upload',
				filesArr: [],
				user_id: 0,
				show: false,
				list1: [],
				list_goods: [],
				page:1,
				customStyle: {
					marginTop: '50px', // 注意驼峰命名，并且值必须用引号包括，因为这是对象
					color: '#333',
					border: 'none',
					background: '#e0ebdd',
				},
				form: {
					id: '',
					name: '',
					num: '',
					goods_ids: '',
					fileList: [],
					goods_num: [],
				},
				rules: {
					name: [
						{ 
							required: true, 
							message: '请输入套餐名称', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change'],
						}
					],
					mobile: [
						{
							required: true, 
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// this.$u.test.mobile()就是返回true或者false的
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change','blur'],
						}
					]
				}
			};
		},
		onLoad(e) {
			let query = e || {};
			this.user_id = query.user_id || 0;
			this.is_update = true;
			// this.getArchivesList();
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			getListGoods: async function() {
				let data = {
					id: this.form.id
				};
				let res = await this.$api.TcJiGoodsList(data);
				console.log(res, 'res')
				if (res.data.length) {
					this.list_goods = res.data;
				}
			},
			handleSearchInput1: async function(val){
				var that = this;
				if (val) {
					let data = {
						keyword:val
						// member_id: this.user_id 
					};
					that.list1 = [];
					let res = await this.$api.getGoodsList(data);
					console.log(res,'123')
					that.list1 = res.data;
					this.show = true
				}
			},
			getArchivesList: async function() {
				let data = {
					page: this.page,
					member_id: this.user_id 
				};
				let res = await this.$api.myjiaddList(data);
				console.log(res,'1111')
				this.list1 = this.list1.concat(res.data);
				
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					var iid = this.user_id;
					if (valid) {
						this.$api.addJiAdd({ 
							user_id:this.user_id,
							name: this.form.name,
							num: this.form.num,
							// goods_num: this.form.goods_num,
							id: this.form.id,
							fileList: this.form.fileList,
						}).then(res => {
							if(res.code == 1){
								uni.showToast({
									title: '添加成功',
									icon: 'success',
									duration: 2000,
									success: function (res1) {
										setTimeout(() => {
											uni.redirectTo({
												url: '/pages/my/user_ji?user_id='+iid
											});
										}, 2000);
									},
								});

							}
							
						});
						
					}
				});
			},
			checkboxGroupChange(e) {
				console.log(e,'ee')
				var str = e.toString();
				this.form.goods_ids = str;
			},
			 // 上传成功回调
			handleSuccess(res, index, lists) {

				if(res.code == 1){
					uni.showToast({
						title: '上传成功',
						icon: 'success'
					});
					// 更新文件列表
					this.form.fileList = this.form.fileList.concat(res.data.url);
					console.log(this.form.fileList,'concat')
				}else{
					uni.showToast({
						title: res.msg,
						icon: 'error'
					});
				}
				
			},
			// 文件被移除
			handleRemove(index, lists) {
				this.form.fileList.splice(index, 1);
				console.log(this.form.fileList,'fileList')
			},
			confirm(e) {
				console.log(e,'11')
				this.form.name = e[0].label;
				this.form.id = e[0].value;
				// this.getListGoods();
			}
		}
	};
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.content{
		width: 90%;margin: 10px auto;
	}
</style>
